package top.fengleifeng.question;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-09-07 10:20
 **/
public class Num367有效的完全平方数 {
    /**
     * 二分查找
     */
    class Solution {
        public boolean isPerfectSquare(int num) {
            if (num < 1) {
                return false;
            } else if (num == 1) {
                return true;
            }
            long numL =  num;
            long left = 2;
            long right = numL / 2;
            while (left <= right) {
                long mid = left + (right - left) / 2;
                long val = mid * mid;
                if (val == numL) {
                    return true;
                } else if (val < numL) {
                    left = mid + 1;
                } else if (val > numL) {
                    right = mid - 1;
                }
            }
            return false;
        }
    }

    public static void main(String[] args) {
        boolean perfectSquare = new Num367有效的完全平方数().new Solution().isPerfectSquare(808201);
        System.out.println(perfectSquare);
    }
}
